#include "UdpServer.hpp"
#include <memory>
#include "Common.hpp"
#include <cstdio>
void Usage(std::string proc)
{
    //bind 单ip版本
    // std::cout<<"Usage: \n\t"<<proc<< " local_ip local_port\n";

    std::cout<<"Usage: \n\t"<<proc<< "local_port\n";
}
 

std:: string OnMessage(const std::string msg)
{
    return  "  && Server: I Got It : "+  msg;
}



//可以传输指令过来执行
std:: string ExecuteCommand(const std::string& request)
{
    std::cout<<"Get a Message:"<<request<<std::endl;
    FILE* fp = popen(request.c_str(),"r");
    if(fp==nullptr)
    {
        return std::string() + "execute error,resson is unkown\n";
    }

    std::string response;
    char buffer[1048];

    while(1)
    {
        char *s = fgets(buffer,sizeof(buffer),fp);
        if(!s)  break;
        else response += buffer;
    }

    pclose(fp);
    return response.empty()?"success":response;
}


int main(int argc,char*argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return Usage_error;
    } 
    // std::string ip = argv[1];
    // uint16_t port = std::stoi(argv[2]);
    // std::unique_ptr<UdpServer> usvr = std::make_unique<UdpServer>(ip,port);

    uint16_t port = std::stoi(argv[1]);
    std::unique_ptr<UdpServer> usvr = std::make_unique<UdpServer>(ExecuteCommand,port);

    usvr->Init();
    usvr->Start();
    return 0;
}